package com.wm.blog.center.service.impl;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.blog.center.common.utils.R;
import com.wm.blog.center.common.utils.login.StpAdminUtil;
import com.wm.blog.center.dao.MemberDao;
import com.wm.blog.center.entity.MemberEntity;
import com.wm.blog.center.service.MemberService;
import org.springframework.stereotype.Service;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public void doSome() {
        System.out.println("dosome");
    }

    @Override
    public Pair<Boolean, String> adminLogin(String username, String password) {

        QueryWrapper<MemberEntity> memberEntityQueryWrapper = new QueryWrapper<>();
        memberEntityQueryWrapper
                // type == 1 == 管理员
                // TODO 使用枚举
                .eq("type", 1)
                .and(wrapper -> {
                    wrapper.eq("login_name", username)
                            .or()
                            // TODO 手机号要加密
                            .eq("phone", username)
                            .or()
                            .eq("email", username);
                });
        MemberEntity memberEntity = this.getOne(memberEntityQueryWrapper);
        if (ObjectUtil.isNull(memberEntity)){
            return Pair.of(false, "用户名错误！");
        }
        if (!memberEntity.getLoginPassword().equals(password)){
            return Pair.of(false, "密码错误！");
        }
        StpAdminUtil.setLoginId(memberEntity.getId());
        StpAdminUtil.setLoginId(memberEntity.getId());
        return Pair.of(true, StpAdminUtil.getTokenValue());
    }
}